package com.jams.goodox.activity.login;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.EditTextWithDel;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by HP on 2018/5/12.
 * 修改密码
 * api/member/editPwd
 */

public class ResetPwdActivity extends BaseActivity {
    private static final String TAG = "ResetPwdActivity";

    private static final String TEL_LENGTH = "请输入11位手机号";
    private static final String CODE_NOT_HAVE = "请输入验证码";
    private static final String PASS_NOT_HAVE = "请输入6-12位密码";
    private static final String PASS_FORMAT = "密码请使用6-12位英文或数字";

    @Bind(R.id.edit_userPass)
    EditTextWithDel edit_userPass;
    @Bind(R.id.edit_userPass2)
    EditTextWithDel edit_userPass2;

    @Bind(R.id.edit_userPass3)
    EditTextWithDel edit_userPass3;

    @Bind(R.id.img_login_seePass)
    ImageView img_login_seePass;

    @Bind(R.id.img_login_seePass2)
    ImageView img_login_seePass2;

    @Bind(R.id.img_login_seePass3)
    ImageView img_login_seePass3;

    @Bind(R.id.btn_submit)
    TextView btn_submit;

    private boolean isPassShow = false;//标示密码是否可见
    private boolean isPassShow2 = false;//标示密码是否可见
    private boolean isPassShow3 = false;//标示密码是否可见

    @Override
    protected int getLayoutId() {
        return R.layout.activity_findpwd;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "修改密码");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部
    }

    @Override
    protected void onPause() {
        super.onPause();
        misLoading();
    }

    @OnClick({R.id.ler_back,  R.id.btn_submit,R.id.img_login_seePass,R.id.img_login_seePass2})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                resetPwd();
                break;

            case R.id.img_login_seePass:
                isPassShow = !isPassShow;
                if (isPassShow) {
                    img_login_seePass.setImageResource(R.mipmap.see_pass_icon);
                    edit_userPass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    img_login_seePass.setImageResource(R.mipmap.not_see_pass_icon);
                    edit_userPass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                edit_userPass.setSelection(edit_userPass.length());
                break;


            case R.id.img_login_seePass2:
                isPassShow2 = !isPassShow2;
                if (isPassShow2) {
                    img_login_seePass2.setImageResource(R.mipmap.see_pass_icon);
                    edit_userPass2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    img_login_seePass2.setImageResource(R.mipmap.not_see_pass_icon);
                    edit_userPass2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                edit_userPass2.setSelection(edit_userPass2.length());
                break;


            case R.id.img_login_seePass3:
                isPassShow3 = !isPassShow3;
                if (isPassShow3) {
                    img_login_seePass3.setImageResource(R.mipmap.see_pass_icon);
                    edit_userPass3.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    img_login_seePass3.setImageResource(R.mipmap.not_see_pass_icon);
                    edit_userPass3.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                edit_userPass3.setSelection(edit_userPass3.length());
                break;

        }
    }

    //重置密码
    private void resetPwd() {
        String pwd = edit_userPass.getText().toString().trim();
        if (TextUtils.isEmpty(edit_userPass.getText())) {
            ToastUtil.showShort(this, PASS_NOT_HAVE);
            return;
        }

        if (pwd.length() < 6 && pwd.length() > 18) {
            ToastUtil.showShort(this, PASS_NOT_HAVE);
            return;
        }

        if(TextUtils.isEmpty( edit_userPass2.getText())){
            ToastUtil.showShort(this, "请输入新密码");
            return;
        }

        String pwd2 = edit_userPass2.getText().toString().trim();
        String pwd3 = edit_userPass3.getText().toString().trim();
        if(!pwd2.equals(pwd3)){
            ToastUtil.showShort(this, "两次密码不一致");
            return;
        }

        clearMap();
        StringBuilder pData = new StringBuilder();
        pData.append("oldPassword:" + pwd)
                .append(",newPassword:" + pwd2)
                .append(",mobile:" + SePref.getPrePhone());

        params.put("requestData", "{" + pData.toString() + "}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);

        showLoading();

        Call<ResponseBody> example = createCallService(LoginService.class).resetPwd(body);

        example.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "data = " + data);
                    JSONObject jsonObject = new JSONObject(data);

                    if(200 == jsonObject.getInt("code")
                            && "true".equals(jsonObject.getString("responseData"))){
                        ToastUtil.show("密码修改成功" );

                        startActivity(new Intent(ResetPwdActivity.this,LoginActivity.class));
                        finish();

                    }else {
                        if(jsonObject.has("error")){
                            ToastUtil.show("error:" + jsonObject.getString("error") );
                        }else {
                            ToastUtil.show("网络异常！" );
                        }
                    }
                } catch (Exception e) {
                    ToastUtil.show("Exception:" + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                Log.d(TAG,"onFailure = " + t.getMessage());
            }
        });

        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}
